package com.zzy.product.controller;

import com.zzy.product.entity.AttrEntity;
import com.zzy.product.service.AttrAttrgroupRelationService;
import com.zzy.product.service.AttrService;
import com.zzy.product.service.CategoryService;
import com.zzy.product.vo.AttrGroupRelationVo;
import com.zzy.product.vo.AttrGroupWithAttrsVo;
import com.zzy.utils.utils.PageUtils;
import com.zzy.utils.utils.R;
import com.zzy.product.entity.AttrGroupEntity;
import com.zzy.product.service.AttrGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 属性分组
 *
 * @author kazu
 * @email kazu_zhang@163.com
 * @date 2023-02-01 21:39:21
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
	@Autowired
	private AttrGroupService attrGroupService;
	
	@Autowired
	private CategoryService categoryService;
	
	@Autowired
	private AttrService attrService;
	
	@Autowired
	private AttrAttrgroupRelationService relationService;
	
	/**
	 * /product/attrgroup/{catelogId}/withattr
	 * @return
	 */
	@GetMapping("/{catelogId}/withattr")
	public R getAttrGroupWithAttr(@PathVariable Long catelogId) {
		/*1、查出当前分类下的所有属性分组*/
		/*2、查出每个属性的所有分组*/
		List<AttrGroupWithAttrsVo> vos = attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);
		return R.ok().put("data", vos);
	}
	
	/**
	 * /product/attrgroup/attr/relation
	 * @return
	 */
	@PostMapping("/attr/relation")
	public R addRelation(@RequestBody List<AttrGroupRelationVo> vos) {
		relationService.saveBatch(vos);
		return R.ok();
	}
	
	/**
	 * 获取属性分组没有关联的其他属性
	 * /product/attrgroup/{attrgroupId}/noattr/relation
	 * @return
	 */
	@GetMapping("/{attrgroupId}/noattr/relation")
	public R noattrRelation(@RequestParam Map<String, Object> params,
							@PathVariable("attrgroupId") Long attrgroupId) {
		PageUtils pageUtils = attrService.getNoAttrRelation(attrgroupId, params);
		return R.ok().put("page", pageUtils);
	}
	
	/**
	 * /attr/relation/delete
	 */
	@PostMapping("/attr/relation/delete")
	public R relationDelete(@RequestBody AttrGroupRelationVo[] vos) {
		attrService.deleteRelation(vos);
		return R.ok();
	}
	
	/**
	 * /api/product/attrgroup/{attrgroupId}/attr/relation
	 * @return
	 */
	@GetMapping("/{attrgroupId}/attr/relation")
	public R attrRelation(@PathVariable Long attrgroupId) {
		List<AttrEntity> data = attrService.getRelationInfo(attrgroupId);
		return R.ok().put("data", data);
	}
	
	/**
	 * 列表
	 */
	@RequestMapping("/list/{catelogId}")
	public R list(@RequestParam Map<String, Object> params, @PathVariable Long catelogId) {
//		PageUtils page = attrGroupService.queryPage(params);
		PageUtils page = attrGroupService.queryPage(params, catelogId);
		
		return R.ok().put("page", page);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{attrGroupId}")
	public R info(@PathVariable("attrGroupId") Long attrGroupId) {
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
		Long catelogId = attrGroup.getCatelogId();
		Long[] path = categoryService.findCatelogPath(catelogId);
		attrGroup.setCatelogPath(path);
		return R.ok().put("attrGroup", attrGroup);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	public R save(@RequestBody AttrGroupEntity attrGroup) {
		attrGroupService.save(attrGroup);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	public R update(@RequestBody AttrGroupEntity attrGroup) {
		attrGroupService.updateById(attrGroup);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	public R delete(@RequestBody Long[] attrGroupIds) {
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));
		
		return R.ok();
	}
	
}
